#include "pointview.h"
#include <QFile>
#include <QHBoxLayout>
#include <QApplication>
#include <QLabel>
#include <QMovie>
#include <QEvent>
#include <QBitmap>
#include "common.h"
#include "huaqimovie.h"

extern int interval_unequal_offset;
extern int all_unequal_offset;

int PointView::m_id = qRegisterMetaType<PointView*>();

PointView::PointView(QWidget *parent)
    :ViewObject(parent)
    ,m_paramPointView(NULL)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_DeleteOnClose);

#ifdef __linux__
    setFocusPolicy(Qt::NoFocus);
#else
    setFocusPolicy(Qt::StrongFocus);
#endif

    if (m_objectParam == NULL)
        m_objectParam = new ParamObject;

    if (m_paramPointView == NULL)
        m_paramPointView = new PointViewParam;
}

PointView::~PointView()
{
    if (m_paramPointView != NULL)
        delete m_paramPointView;

    if (m_objectParam != NULL)
        delete  m_objectParam;

    m_objectParam = NULL;
    m_paramPointView = NULL;
}

void PointView::SetOffset(int offset)
{
    if (offset == 0) return;
    /*设置不等间距的偏移量*/
    int stid = m_objectParam->GetStId();
    int curr_id = mProtocolData->curr_or_next_id;
    int start_id = mProtocolData->start_id;
    int stop_id = mProtocolData->stop_id;
    int rundir = RUN_INVALID;
    if (start_id < stop_id)
        rundir = RUN_UP;
    else if (start_id > stop_id)
        rundir = RUN_DOWN;

    bool is_mirror = mProtocolData->file_path.contains("mirror");

    //当前站都是偏移一个偏移量
    if (curr_id == stid)
        mOffsetX = offset;


    //当前站后面的站都是偏移两个偏移量
    //上行或者下行镜像一组(大号在右侧)，下行或者上行镜像一组(小号在右侧)
    if ((is_mirror && rundir == RUN_DOWN) || (!is_mirror && rundir == RUN_UP))
    {
        if (stid > curr_id)
            mOffsetX = 2*offset;
    }
    else
    {
        if (stid < curr_id)
            mOffsetX = 2*offset;
    }
    /**************************************************/
}

void PointView::ShowObject()
{ 

    if (!m_paramPointView || !m_objectParam) return;

    int stid = m_objectParam->GetStId();
    LoadStationPic(stid, mFilename);
    url = m_paramPointView->m_resNormalPath;

    int interval_config_offset = interval_unequal_offset;
    int all_config_offset = all_unequal_offset;

    if (mProtocolData)
    {
        int curr_id = mProtocolData->curr_or_next_id;
        int start_id = mProtocolData->start_id;
        int stop_id = mProtocolData->stop_id;
        int rundir = RUN_INVALID;
        if (start_id < stop_id)
            rundir = RUN_UP;
        else if (start_id > stop_id)
            rundir = RUN_DOWN;

        if (mFilename.contains(IntervalFlag))
            SetOffset(interval_config_offset);
        if (mFilename.contains(AllFlag))
            SetOffset(all_config_offset);

        if (curr_id == INVALID_NUM || stid == 0 || curr_id == 0)
        {
            url = m_paramPointView->m_resNormalPath;
        }
        else
        {
            if (curr_id == stid)
            {
                url = m_paramPointView->m_resCurrentPath;
            }
            else if (stid > curr_id)
            {
                if (rundir == RUN_UP)
                {
                    url =  m_paramPointView->m_resFuturePath;
                }
                else if (rundir == RUN_DOWN)
                {
                    url =  m_paramPointView->m_resPassPath;
                }
            }
            else if (stid < curr_id)
            {
                if (rundir == RUN_UP)
                {
                    url = m_paramPointView->m_resPassPath;
                }
                else if (rundir == RUN_DOWN)
                {
                    url =  m_paramPointView->m_resFuturePath;
                }
            }
            else
            {
                url = m_paramPointView->m_resNormalPath;
            }
        }

        foreach (int id, mProtocolData->skip_list)
        {
            if (id == stid)
            {
                url = m_paramPointView->m_resSkipPath;
                break;
            }
        }

        if (rundir == RUN_UP)
        {
            if (url.contains("all_line") && stid == start_id)
            {
                url = m_paramPointView->m_resCrossPath;
            }
            if (stid < start_id || stid > stop_id)
            {
                url = m_paramPointView->m_resCrossPath;
            }
        }
        else if (rundir == RUN_DOWN)
        {
            if (url.contains("all_line") && stid == start_id)
            {
                url = m_paramPointView->m_resCrossPath;
            }
            if (stid > start_id || stid < stop_id)
            {
                url = m_paramPointView->m_resCrossPath;
            }
        }
    }
    else
    {
        url = m_paramPointView->m_resNormalPath;
    }

    ShowUrl();
}

void PointView::LoadStationPic(int stid, QString filename)
{
    if (filename == "")
        return;

    QString file_name = filename.left(filename.size() - 4);
    QStringList list = file_name.split("_");
    QString flag = list.at(ParamType);

    m_paramPointView->m_resNormalPath  = QString("%1_point_future_%2").arg(flag).arg(stid);
    m_paramPointView->m_resFuturePath  = QString("%1_point_future_%2").arg(flag).arg(stid);
    m_paramPointView->m_resSkipPath    = QString("%1_point_skip_%2").arg(flag).arg(stid);
    m_paramPointView->m_resCurrentPath = QString("%1_point_current_%2").arg(flag).arg(stid);
    m_paramPointView->m_resCrossPath   = QString("%1_point_cross_%2").arg(flag).arg(stid);
    m_paramPointView->m_resPassPath    = QString("%1_point_pass_%2").arg(flag).arg(stid);
}

void PointView::GetAttribute(std::string &attribute)
{   
    std::string jsonStr;
    Json::Value base;
    Json::StreamWriterBuilder writerBuilder;
    std::ostringstream os;

    base["zindex"] = QString::number(m_objectParam->GetIndex()).toStdString();
    base["stid"] = QString::number(m_objectParam->GetStId()).toStdString();
    base["x"] = QString::number(m_objectParam->GetX()).toStdString();
    base["y"] = QString::number(m_objectParam->GetY()).toStdString();
    base["w"] = QString::number(m_objectParam->GetWidth()).toStdString();
    base["h"] = QString::number(m_objectParam->GetHeight()).toStdString();
    base["name"] = m_objectParam->GetName().toStdString();
    std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());
    jsonWriter->write(base, &os);
    jsonStr = os.str();

    attribute = jsonStr;
}

void PointView::SetAttribute(std::string jsonStr)
{
    bool res;
    JSONCPP_STRING errs;
    Json::Value root;
    Json::CharReaderBuilder readerBuilder;
    std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
    res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str()+jsonStr.length(), &root, &errs);

    if (!res || !errs.empty())
    {
         LOG_QDEBUG<<__FUNCTION__<<"json parse faild"<<QString::fromStdString(errs);
    }

    if (CheckJsonValueType(root, "zindex", Json::stringValue))
    {
         m_objectParam->SetIndex(QString::fromStdString(root["zindex"].asString()).toInt());
    }

    if (CheckJsonValueType(root, "stid", Json::stringValue))
    {
        m_objectParam->SetStId(QString::fromStdString(root["stid"].asString()).toInt());
    }
    if (CheckJsonValueType(root, "x", Json::stringValue))
    {
        m_objectParam->SetX(QString::fromStdString(root["x"].asString()).toInt());
    }
    if (CheckJsonValueType(root, "y", Json::stringValue))
    {
        m_objectParam->SetY(QString::fromStdString(root["y"].asString()).toInt());
    }
    if (CheckJsonValueType(root, "w", Json::stringValue))
    {
        m_objectParam->SetWidth(QString::fromStdString(root["w"].asString()).toInt());
    }
    if (CheckJsonValueType(root, "h", Json::stringValue))
    {
        m_objectParam->SetHeight(QString::fromStdString(root["h"].asString()).toInt());
    }

    if (CheckJsonValueType(root, "name", Json::stringValue))
    {
        m_objectParam->SetName(QString::fromStdString(root["h"].asString()));
    }
}

void PointView::ParseXML(QDomElement* xmlDomEle)
{
    m_objectParam->SetIndex((xmlDomEle->attribute("zindex","0")).toInt());
    m_objectParam->SetStId((xmlDomEle->attribute("stid","0")).toInt());
    m_objectParam->SetX((xmlDomEle->attribute("x","0")).toInt());
    m_objectParam->SetY((xmlDomEle->attribute("y","0")).toInt());
    m_objectParam->SetWidth((xmlDomEle->attribute("w","0")).toInt());
    m_objectParam->SetHeight((xmlDomEle->attribute("h","0")).toInt());
    m_objectParam->SetName(xmlDomEle->attribute("name",""));
}

void PointView::PacketXML(QDomElement* dom)
{
    QDomDocument document;
    *dom = document.createElement("item");
    dom->setAttribute("type", m_objectParam->GetObjectName());
    dom->setAttribute("zindex",QString::number(m_objectParam->GetIndex()));
    dom->setAttribute("stid",QString::number(m_objectParam->GetStId()));
    dom->setAttribute("x",QString::number(m_objectParam->GetX()));
    dom->setAttribute("y",QString::number(m_objectParam->GetY()));
    dom->setAttribute("w",QString::number(m_objectParam->GetWidth()));
    dom->setAttribute("h",QString::number(m_objectParam->GetHeight()));
    dom->setAttribute("name",m_objectParam->GetName());
}


